package com.cyz.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyz.dao.DoctorDao;
import com.cyz.dao.UserDao;
import com.cyz.entity.Doctor;
import com.cyz.entity.Old;
import com.cyz.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/doctor")
public class DoctorController {

    @Autowired
    private DoctorDao doctorDao;

    @Autowired
    private UserDao userDao;

    @PostMapping("/query")
    public List<Doctor> queryDoctor(){
        List<Doctor> doctors = doctorDao.selectList(new QueryWrapper<Doctor>());
        return doctors;
    }

    @PostMapping("/queryByCon")
    public List<Doctor> queryByCon(String id, String name, String job){
        QueryWrapper<Doctor> wrapper = new QueryWrapper<>();
        if (!id.equals("")){
            int i = Integer.parseInt(id);
            if (i > 0) {
                wrapper.eq("id",i);
            }
        }
        if (name != null && !name.equals("")){
            wrapper.eq("name", name);
        }
        if (job != null && !job.equals("")){
            wrapper.eq("job", job);
        }
        List<Doctor> doctors = doctorDao.selectList(wrapper);
        return doctors;
    }

    @PostMapping("/getDocId")
    public List<Integer> getDocId(){
        return doctorDao.selectList(new QueryWrapper<Doctor>()).stream().map(item -> item.getId()).collect(Collectors.toList());
    }

    @PostMapping("/getDocById/{id}")
    public Doctor getDocById(@PathVariable int id){
        return doctorDao.selectById(id);
    }

    @PostMapping("/updateDoc")
    public String updateDoc(Doctor doctor){
        System.out.println(doctor.getId());
        System.out.println(doctor.getName());
        int update = doctorDao.update(doctor, new QueryWrapper<Doctor>().eq("id", doctor.getId()));
        return update == 0 ? "更新失败" : "更新成功";
    }

    @PostMapping("/deleteDoc/{id}")
    public String deleteDoc(@PathVariable int id){
        Doctor doctor = doctorDao.selectById(id);
        int delete = doctorDao.deleteById(id);
        if (delete != 0){
            String name = doctor.getName();
            userDao.delete(new QueryWrapper<User>().eq("name",name));
        }
        return delete == 0 ? "删除失败" : "删除成功";
    }
}
